/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.Service;
import java.util.*;
//import ge.info.domain.DbExecutor;
import ge.info.domain.transport.TransportType;
import ge.info.logging.Session;
import ge.info.sip.SessionMap;
import ge.ktapestry.pages.BasePageExt;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.PageRedirectException;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageValidateListener;


public abstract class Main extends BasePageExt implements PageBeginRenderListener, PageValidateListener
{

     public final static Integer MALE = new Integer(1);
    public final static Integer FAME = new Integer(0);
    // public final static Integer NOSEX = new Integer(-1);


    public static int SERVICE_ID = 0;

    public abstract String getSmtxt();

    public abstract void setSmtxt(String dtxt);

    public abstract Service getLoopService();

    public abstract void setLoopService(Service loopService);

    public abstract TransportType getTransportType();

    public abstract void setTransportType(TransportType transportType);

    public abstract String getCurrentTime();

    public abstract void setCurrentTime(String s);

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getRequest();
//    @InjectState("user")
//    public abstract User getUser();
//
//    public abstract void setUser(User User);

    @InjectObject("spring:CurrentSessions")
    public abstract SessionMap getCurrentSessions();

    public abstract int getLw();
    public abstract void setLw(int lw);

    public abstract String getRandomNumber();
    public abstract void setRandomNumber(String str);

    public abstract String getCphone();
    public abstract void setCphone(String cphone);

    public abstract String getLnote();
    public abstract void setLnote(String lnote);

    public abstract List<TreeMap> getMenMessages();

    public abstract void setMenMessages(List<TreeMap> list);

    public abstract TreeMap getMessage();

    public abstract void setMessage(TreeMap message);

    
@Persist
    public abstract String getDnm();
    public abstract void setDnm(String dnm);
@Persist
    public abstract Integer getSex();
    public abstract void setSex(Integer sex);


    public void pageBeginRender(PageEvent event)
    {
        super.pageEndRender(event);

        if (getSession().getCallType()!=-1 && getRequest().getParameter("rn") != null)
        {
            setRandomNumber(getRequest().getParameter("rn"));
            event.getRequestCycle().activate(this);
        }


        if (!event.getRequestCycle().isRewinding())
        {
            if (getSession().getCallType()!=-1 && getRequest().getParameter("rn") != null)

            {
                
            setRandomNumber(getRequest().getParameter("rn"));
             event.getRequestCycle().activate(this);
            }

            this.setMenMessages(getDbExecutor().getMenMessages());
    
    // setSex(NOSEX);
        }
        
    }

    @Override
    public void pageValidate(PageEvent event)
    {
        if (getSession() == null || getSession().getUserName() == null)
        {
            Session session = (Session) getCurrentSessions().get(getRequest().getLocalAddr());
            if (session == null)
            {
                throw new PageRedirectException("Home");
            }
        }
    }

    public void openOrgSearchPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("OrgSearch");
           
            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openCityCodesPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("CityCodes");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openTransportTypePage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("TransportTypePage");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openIncityTransportPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("IncityTransport");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openCurrencyPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("CurrencyPage");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openSearchStreetPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("SearchStreet");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openGeoIndPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("GeoInd");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

     public void openCityDistancesPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("CityDistances");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openTimePage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("Time");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

       public void openWeathersPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("Weathers");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openSecularCalendarPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("SecularCalendar");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openSitePage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("SitePage");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openSportTypePage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("SportTypePage");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openChurchCalendarPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("ChurchCalendar");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

      public void openOthersPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("OthersPage");

            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void openTransportPage(IRequestCycle cycle)
    {
        try
        {
            IPage page = cycle.getPage("TransportTypePage");
            PropertyUtils.setProperty(page, "transportType", getTransportType());
            cycle.activate(page);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void openServicePage(IRequestCycle cycle, Service loopService)
    {
        getDbExecutor().populateServiceDetails(loopService);
        Service serv = new Service();
        serv.setServiceID(loopService.getServiceID());
        serv.setName(loopService.getName());
        serv.setServiceDetails(loopService.getServiceDetails());
        serv.setAddressSearch(loopService.getAddressSearch());
        IPage page = cycle.getPage("ServicePage");
        try
        {
            PropertyUtils.setProperty(page, "service", serv);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        cycle.activate(page);
    }

    public void searchInd(IRequestCycle cycle, Integer id)
    {
        IPage page = cycle.getPage("SearchInd");
        try
        {
            PropertyUtils.setProperty(page, "searchType", id);
            PropertyUtils.setProperty(page, "abonentsFound", null);

        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        cycle.activate(page);
    }

    public void getTime(IRequestCycle cycle)
    {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
        setCurrentTime(simpleDateFormat.format(new Date(System.currentTimeMillis())));
    }
    public void openSchedulePage(IRequestCycle cycle)
    {
        IPage p = cycle.getPage("ScheduleTypePage");

        org.apache.hivemind.util.PropertyUtils.write(p, "schedulePlaceCurrent",null);
        cycle.activate(p);
    }
     public void submittr(IRequestCycle cycle)
    {
      getDbExecutor().updtr(getSession(), getCphone(),getLnote());
     }
     public void submitfl(IRequestCycle cycle)
    {
         getDbExecutor().updfl(getSession());
     }
public void submitmb(IRequestCycle cycle)
    {
        
if (getSex()!=null)
{
        setSex(getSex());
       getDbExecutor().insertMobName(getSession(),getDnm(),getSex());
        setDnm(null);
}
}
 public void sendSMS(IRequestCycle cycle, String smtxt)
    {


       StringBuilder sb = new StringBuilder();
        if (getSmtxt()!=null)
        {

        sb.append(getSmtxt());


        }



        getDbExecutor().insertSms(getSession(), sb.toString(),SERVICE_ID);
    }
    
}
